//
//  NSObject+Category.m
//  CookeeBike
//
//  Created by 谷建军 on 16/2/25.
//  Copyright © 2016年 Ada. All rights reserved.
//

#import "NSObject+Category.h"

#define IsNull(obj)  (obj == nil || [obj isKindOfClass:[NSNull class]]) ? YES : NO

@implementation NSObject (Category)

@end  // NSObject


@implementation NSString (SafeJsonString)

+ (NSString *)safeJsonString:(id)obj {
    if (IsNull(obj)) {
        return @"";
    }
    return (NSString *)obj;
}

- (NSString *(^)(NSString *))sk_appendString {
    return ^(NSString *string){
        return [NSString stringWithFormat:@"%@%@",self,string];
    };
}

- (BOOL (^)(NSString *))sk_equalToString {
    return ^(NSString *string){
        return [self isEqualToString:string];
    };
}

@end  // SafeJsonString


@implementation NSNumber (SafeJonsNumber)

+ (int)safeJsonInt:(id)obj {
    if (IsNull(obj)) {
        return 0;
    }
    return [obj intValue];
}

+ (NSInteger)safeJsonInteger:(id)obj {
    if (IsNull(obj)) {
        return 0;
    }
    return [obj integerValue];
}

+ (CGFloat)safeJsonFloat:(id)obj {
    if (IsNull(obj)) {
        return 0.0;
    }
    return [obj floatValue];
}

+ (CGFloat)safeJsonDouble:(id)obj {
    if (IsNull(obj)) {
        return 0.0;
    }
    return [obj doubleValue];
}

@end  // SafeJonsNumber
